package com.sanjiahantang.youjiaclient.module.decorate.views.impl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpFragment;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.module.login.views.impl.LoginActivity;
import com.sanjiahantang.youjiaclient.module.common.views.impl.MainActivity;
import com.sanjiahantang.youjiaclient.module.complain.views.impl.ComplainActivity;
import com.sanjiahantang.youjiaclient.module.decorate.adapters.FlowAdapter;
import com.sanjiahantang.youjiaclient.module.decorate.beans.DecorateManageBean;
import com.sanjiahantang.youjiaclient.module.decorate.presenters.DecorateManagePresenter;
import com.sanjiahantang.youjiaclient.module.decorate.views.DecorateManageView;
import com.sanjiahantang.youjiaclient.module.programmeprice.views.impl.ProgramOrderActivity;
import com.sanjiahantang.youjiaclient.module.programmeprice.views.impl.ProgramPriceActivity;
import com.sanjiahantang.youjiaclient.umengshare.UmengShareManager;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;

import static com.sanjiahantang.youjiaclient.module.programmeprice.views.ProgramPriceView.TYPE_INCREASE;
import static com.sanjiahantang.youjiaclient.module.programmeprice.views.ProgramPriceView.TYPE_REDUCE;


/**
 * Created by zy on 2017/8/11.
 */

public class DecorateDetailFragment extends BaseMvpFragment<DecorateManageView, DecorateManagePresenter> implements DecorateManageView, View.OnClickListener {

    private View $view;
    private Activity mContext;

    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private MagicIndicator miTabs;
    private SwipeRefreshLayout refreshLayout;
    private ImageView ivArcher;
    private View rlButtons,vsNetworkError;

    public void setCurrentPiId(int currentPiId) {
        this.currentPiId = currentPiId;
    }

    private int currentPiId;


    //装修id
    private DecorateManageBean.Data decorateData;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if ($view != null) {
            ViewGroup parent = (ViewGroup) $view.getParent();
            if (parent != null) {
                parent.removeView($view);
            }
        } else {
            $view = inflater.inflate(R.layout.fragment_recycler_list, container, false);
        }
        return $view;
    }


    @Override
    public void onResume() {
        super.onResume();
        if (Utils.NO_NETWORK_STATE == Utils.isNetworkAvailable(context)) {
            onNetError(true);
            return;
        }else{
            onNetError(false);
        }

        if (TextUtils.isEmpty(ConfigValue.DATA_KEY)) {
            Intent intent = new Intent(context, LoginActivity.class);
            startActivityForResult(intent, KEY_DECORATE);
            ((MainActivity) context).setFragmentIndex(0);
        } else {

            if (ConfigValue.param != null && ConfigValue.param instanceof Integer) {
                currentPiId = (int) ConfigValue.param;
                ConfigValue.param = null;
            }
            presenter.loadTab(context, currentPiId, new BindTabListener() {

                @Override
                public void bindAdapter(CommonNavigatorAdapter adapter) {
                    final CommonNavigator commonNavigator = new CommonNavigator(context);
                    commonNavigator.setAdapter(adapter);
                    miTabs.setNavigator(commonNavigator);
                }

                @Override
                public void switchPages(int index) {
                    miTabs.onPageSelected(index);
                }

                @Override
                public void reload(int piId) {
                    currentPiId = piId;
                    onResume();
                }

                @Override
                public void changeFragment(DecorateManageBean.Data data) {
                    if (data == null) {
                        refreshLayout.setVisibility(View.GONE);
                        $view.findViewById(R.id.rlAction).setVisibility(View.GONE);
                        return;
                    } else {
                        refreshLayout.setVisibility(View.VISIBLE);
                        $view.findViewById(R.id.rlAction).setVisibility(View.VISIBLE);
                        closeButtonPanel();
                    }
                    decorateData = data;
                    currentPiId = decorateData.getPiId();
                    if (decorateData.getPpId() != 0) {
                        $view.findViewById(R.id.rlAction).setVisibility(View.VISIBLE);
                    }
                    if (decorateData != null) {
                        boolean flag = false;
                        if (decorateData.getCompletionList() != null && decorateData.getCompletionList().size() > 0) {
                            if (decorateData.getCompletionList().get(decorateData.getCompletionList().size() - 1).getStatusId() >= 156) {
                                flag = true;
                            }
                        }
                        if (flag) {
                            $view.findViewById(R.id.rlComplain).setVisibility(View.GONE);
                            $view.findViewById(R.id.rlMinus).setVisibility(View.GONE);
                            $view.findViewById(R.id.rlAdd).setVisibility(View.GONE);
                            $view.findViewById(R.id.rlAction).setVisibility(View.GONE);
                        } else {
                            $view.findViewById(R.id.rlAction).setVisibility(View.VISIBLE);
                            if (decorateData.getIsOfficial() == 1){
                                $view.findViewById(R.id.rlComplain).setVisibility(View.VISIBLE);
                                $view.findViewById(R.id.rlAdd).setVisibility(View.VISIBLE);
                            }else{
                                $view.findViewById(R.id.rlComplain).setVisibility(View.GONE);
                                $view.findViewById(R.id.rlAdd).setVisibility(View.GONE);
                            }
                            $view.findViewById(R.id.rlMinus).setVisibility(View.VISIBLE);
                        }
                    }
                    recyclerView.setAdapter(new FlowAdapter(context, decorateData, presenter));
                }
            });

        }
    }




    @Override
    public DecorateManagePresenter initPresenter() {
        return new DecorateManagePresenter();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        miTabs = (MagicIndicator) $view.findViewById(R.id.miTabs);
        TextView title = (TextView) $view.findViewById(R.id.title);
        title.setText("装修管理");
        $view.findViewById(R.id.fl_Left).setOnClickListener(this);
        vsNetworkError = $view.findViewById(R.id.vsNetworkError);

        recyclerView = (RecyclerView) $view.findViewById(R.id.recyclerView);
        mContext = (Activity) getContext();
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        refreshLayout = (SwipeRefreshLayout) $view.findViewById(R.id.refreshLayout);

        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onResume();
            }
        });

        ivArcher = (ImageView) $view.findViewById(R.id.ivArcher);

        $view.findViewById(R.id.rlAction).setOnClickListener(this);
        rlButtons = $view.findViewById(R.id.rlButtons);
        $view.findViewById(R.id.rlComplain).setOnClickListener(this);
        $view.findViewById(R.id.rlAdd).setOnClickListener(this);
        $view.findViewById(R.id.rlMinus).setOnClickListener(this);
        $view.findViewById(R.id.rlShare).setOnClickListener(this);
        //显示按钮
        rlButtons.setVisibility(View.GONE);
        $view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    public void initCreateFragment(DecorateManageBean.Data data) {
        this.decorateData = data;
    }


    @Override
    public void showProgress() {
        context.showProgress(context);
    }

    @Override
    public void hideProgress() {
        if (context == null) return;
        context.hideProgress();
        refreshLayout.setRefreshing(false);
    }

    @Override
    public DecorateManagePresenter getPresenter() {
        return presenter;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                context.finish();
                break;
            case R.id.rlAction:
                if (!"1".equals(ivArcher.getTag(R.id.decode))) {
                    RotateAnimation rotateAnimation = new RotateAnimation(0.0f, +180.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                    rotateAnimation.setDuration(500);
                    rotateAnimation.setFillAfter(true);
                    rotateAnimation.setRepeatCount(0);
                    ivArcher.setTag(R.id.decode, "1");
                    ivArcher.startAnimation(rotateAnimation);

                    rlButtons.setVisibility(View.VISIBLE);
                    TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.5f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
                    translateAnimation.setRepeatCount(0);
                    translateAnimation.setDuration(500);

                    rlButtons.startAnimation(translateAnimation);
                } else {
                    closeButtonPanel();
                }

                break;
            case R.id.rlComplain:
                startActivity(new Intent(context, ComplainActivity.class)
                        .putExtra(KEY_PARAM, decorateData)
                );
                closeButtonPanel();
                break;
            case R.id.rlAdd://增项
                presenter.findIsPushStateId(context,decorateData.getPpId(), new BaseView.OnLoadListener() {
                    @Override
                    public void onLoad(Object content) {
                        if(content != null && content instanceof Boolean){
                            boolean flag = (boolean) content;
                            if (flag){
                                startActivity(new Intent(context, ProgramPriceActivity.class)
                                        .putExtra(KEY_ID, String.valueOf(decorateData.getPpId()))
                                        .putExtra(KEY_TYPE, TYPE_INCREASE).putExtra(KEY_VERSION,"0")
                                );
                            }else{
                                Utils.showToast(context,"您没有新的增项");
                            }
                        }else{
                            Utils.showToast(context,"您没有新的增项");
                        }
                    }
                });
                closeButtonPanel();
                break;
            case R.id.rlMinus://减项
                if(TextUtils.isEmpty(decorateData.getEngineNumber())){
                    Utils.showToast(context,"暂无工程订单");
                }else{
                    if (decorateData.getPiId() > 0){
                        startActivity(new Intent(context, ProgramOrderActivity.class)
                                .putExtra(KEY_ID, decorateData.getPpId())
                                .putExtra(KEY_TYPE, TYPE_REDUCE)
                        );
                    }else{
                        Utils.showToast(context,"您没有减项");
                    }
                }
                closeButtonPanel();
                break;
            case R.id.rlShare:
                UmengShareManager.share(this.getActivity(), "");
                closeButtonPanel();
                break;
        }
    }

    private void closeButtonPanel() {
        RotateAnimation rotateAnimation = new RotateAnimation(+180.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(500);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setRepeatCount(0);
        ivArcher.setTag(R.id.decode, "2");
        ivArcher.startAnimation(rotateAnimation);


        TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.5f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
        translateAnimation.setRepeatCount(0);
        translateAnimation.setDuration(500);
        translateAnimation.setFillAfter(true);

        rlButtons.startAnimation(translateAnimation);
    }

    @Override
    public void onPause() {
        super.onPause();
        closeButtonPanel();
    }

    private void onNetError(boolean b) {
        if (b){
            vsNetworkError.setVisibility(View.VISIBLE);
            vsNetworkError.findViewById(R.id.btnReloadNetwork).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onResume();
                }
            });
        }else{
            vsNetworkError.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
        }
    }
}
